package com.netposa.datawarehouse.statistics.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.netposa.datawarehouse.statistics.vo.HomeVehicleCount;
import com.netposa.datawarehouse.statistics.vo.HomeVehicleParam;
import com.netposa.datawarehouse.statistics.vo.HomeVehicleResp;
import com.netposa.datawarehouse.statistics.vo.PosVO;
import com.netposa.imageserver.client.ImageServerClient;
import com.netposa.imageserver.client.exception.ImageServerException;
import com.netposa.npwonder.base.model.ResponseData;
import com.netposa.npwonder.base.util.BusinessServerId;
import com.netposa.npwonder.base.util.RestAssert;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

@Service
public class HomeService {
   private static final Logger log = LoggerFactory.getLogger(HomeService.class);
   @Autowired
   private RestAssert restAssert;
   @Autowired
   private ImageServerClient imageServerClient;
   private final String dateFormatter = "yyyy-MM-dd HH:mm:ss";

   public HomeVehicleResp zfycCount() throws Exception {
      HomeVehicleResp retVal = new HomeVehicleResp();
      HomeVehicleParam param = new HomeVehicleParam();
      param.setNocturnalRange("2105");
      Date nowTime = new Date();
      param.setStartTime(DateUtils.truncate(DateUtils.addMonths(nowTime, -1), 5).getTime());
      param.setEndTime(DateUtils.truncate(nowTime, 5).getTime());
      ResponseData responseData = this.restAssert.getBusinessAssert(BusinessServerId.pvd).postFormData(this.createPvdParam(param), "/pvd/analysisOnline/nocturnal/home/zfyc");
      if (responseData.code == 200) {
         JSONObject data = responseData.getData();
         JSONArray array = data.getJSONArray("list");
         if (array != null) {
            List list = new ArrayList();
            retVal = new HomeVehicleResp(data.getInteger("count"), list);

            for(int i = 0; i < array.size(); ++i) {
               try {
                  JSONObject jsonObject = array.getJSONObject(i);
                  String plateNumber = jsonObject.getString("plateNumber");
                  String plateColor = jsonObject.getString("plateColor");
                  param.setPlateColor(plateColor);
                  param.setPlateNumber(plateNumber);
                  ResponseData responseData2 = this.restAssert.getBusinessAssert(BusinessServerId.pvd).postFormData(this.createPvdParam(param), "/pvd/analysisOnline/nocturnal/dates");
                  String time = responseData2.getData().getJSONArray("list").getJSONObject(0).getString("nocturnalDate");
                  Date queryDate = DateUtils.parseDate(time, new String[]{"yyyy-MM-dd HH:mm:ss"});
                  String img = this.findzfycImg(queryDate, param);
                  list.add(new HomeVehicleCount(plateNumber, queryDate.getTime(), img));
               } catch (Exception var16) {
                  log.error("昼伏夜出统计异常：{}", var16.getMessage(), var16);
               }
            }
         }
      }

      return retVal;
   }

   private MultiValueMap createPvdParam(HomeVehicleParam param) {
      MultiValueMap formData = new LinkedMultiValueMap();
      formData.setAll(JSONObject.parseObject(JSON.toJSONString(param)));
      return formData;
   }

   private String findzfycImg(Date date, HomeVehicleParam param) {
      Date endDate = DateUtils.addHours(date, 5);
      Date startDate = DateUtils.addDays(endDate, -1);
      param.setStartTime(startDate.getTime());
      param.setEndTime(endDate.getTime());
      JSONObject responseData = this.queryTrafficList(param);
      Object obj = responseData.get("data");
      if (obj != null) {
         List array = (List)obj;
         Map map = (Map)array.get(0);
         return (String)map.get("panoramaImage");
      } else {
         return null;
      }
   }

   private JSONObject queryTrafficList(HomeVehicleParam param) {
      JSONObject responseData = (JSONObject)this.restAssert.getBusinessAssert(BusinessServerId.pvd).postData(param, "/pvd/traffic/traffic/list", JSONObject.class);
      return responseData;
   }

   public HomeVehicleResp illegalCount() throws Exception {
      HomeVehicleResp retVal = new HomeVehicleResp();
      HomeVehicleParam param = new HomeVehicleParam();
      param.setIllegalFrequency(2);
      param.setWzlxStr("allPeccancy");
      param.setPageSize(4);
      Date nowTime = new Date();
      param.setStartTime(DateUtils.truncate(DateUtils.addDays(nowTime, -7), 5).getTime());
      param.setEndTime(DateUtils.truncate(DateUtils.addDays(nowTime, 1), 5).getTime());
      ResponseData responseData = this.restAssert.getBusinessAssert(BusinessServerId.pvd).postFormData(this.createPvdParam(param), "/pvd/traffic/traffic/home/illegal");
      if (responseData.code == 200) {
         JSONObject data = responseData.getData();
         JSONArray array = data.getJSONArray("list");
         if (array != null) {
            List list = new ArrayList();
            retVal = new HomeVehicleResp(data.getInteger("count"), list);

            for(int i = 0; i < array.size(); ++i) {
               try {
                  JSONObject jsonObject = array.getJSONObject(i);
                  String plateNumber = jsonObject.getString("plateNumber");
                  param.setPlateNumber(plateNumber);
                  String img = jsonObject.getJSONObject("imageModel").getString("panoramaImage");
                  list.add(new HomeVehicleCount(plateNumber, this.findIllegalTime(param), img));
               } catch (Exception var12) {
                  log.error("频繁违章统计异常：{}", var12.getMessage(), var12);
               }
            }
         }
      }

      return retVal;
   }

   private Long findIllegalTime(HomeVehicleParam param) throws Exception {
      param.setQueryType(1);
      JSONObject responseData = this.queryTrafficList(param);
      Object obj = responseData.get("data");
      if (obj != null) {
         List array = (List)obj;
         Map map = (Map)array.get(0);
         return DateUtils.parseDate((String)map.get("passTime"), new String[]{"yyyy-MM-dd HH:mm:ss"}).getTime();
      } else {
         return null;
      }
   }

   public HomeVehicleResp kcddhCount() throws Exception {
      HomeVehicleResp retVal = new HomeVehicleResp();
      HomeVehicleParam param = new HomeVehicleParam();
      param.setIllegalType("1");
      param.setOrder("desc");
      param.setQueryType(2);
      Date nowTime = new Date();
      param.setStartTime(DateUtils.truncate(DateUtils.addDays(nowTime, -7), 5).getTime());
      param.setEndTime(DateUtils.truncate(DateUtils.addDays(nowTime, 1), 5).getTime());
      ResponseData responseData = this.restAssert.getBusinessAssert(BusinessServerId.pvd).postFormData(this.createPvdParam(param), "/pvd/traffic/traffic/home/kcddh");
      if (responseData.code == 200) {
         JSONObject data = responseData.getData();
         JSONArray array = data.getJSONArray("list");
         if (array != null) {
            List list = new ArrayList();
            JSONObject countData = (JSONObject)this.restAssert.getBusinessAssert(BusinessServerId.pvd).postData(param, "/pvd/traffic/traffic/count", JSONObject.class);
            Integer count = countData.getInteger("data");
            retVal = new HomeVehicleResp(count, list);

            for(int i = 0; i < array.size(); ++i) {
               JSONObject jsonObject = array.getJSONObject(i);
               String panoramaImage = jsonObject.getString("panoramaImage");
               String featureImage = jsonObject.getString("featureImage");
               list.add(new HomeVehicleCount(jsonObject.getString("plateNumber"), DateUtils.parseDate(jsonObject.getString("passTime"), new String[]{"yyyy-MM-dd HH:mm:ss"}).getTime(), panoramaImage, this.getFeatureImg(featureImage, panoramaImage, jsonObject)));
            }
         }
      }

      return retVal;
   }

   public HomeVehicleResp wxaqdCount() throws Exception {
      HomeVehicleResp retVal = new HomeVehicleResp();
      HomeVehicleParam param = new HomeVehicleParam();
      param.setIllegalType("0");
      param.setOrder("desc");
      param.setQueryType(2);
      Date nowTime = new Date();
      param.setStartTime(DateUtils.truncate(DateUtils.addDays(nowTime, -7), 5).getTime());
      param.setEndTime(DateUtils.truncate(DateUtils.addDays(nowTime, 1), 5).getTime());
      ResponseData responseData = this.restAssert.getBusinessAssert(BusinessServerId.pvd).postFormData(this.createPvdParam(param), "/pvd/traffic/traffic/home/wxaqd");
      if (responseData.code == 200) {
         JSONObject data = responseData.getData();
         JSONArray array = data.getJSONArray("list");
         if (array != null) {
            List list = new ArrayList();
            retVal = new HomeVehicleResp(data.getInteger("count"), list);

            for(int i = 0; i < array.size(); ++i) {
               JSONObject jsonObject = array.getJSONObject(i);
               if (list.size() < param.getPageSize()) {
                  String panoramaImage = jsonObject.getString("panoramaImage");
                  String featureImage = jsonObject.getString("featureImage");
                  list.add(new HomeVehicleCount(jsonObject.getString("plateNumber"), DateUtils.parseDate(jsonObject.getString("passTime"), new String[]{"yyyy-MM-dd HH:mm:ss"}).getTime(), panoramaImage, this.getFeatureImg(featureImage, panoramaImage, jsonObject)));
               }
            }
         }
      }

      return retVal;
   }

   public HomeVehicleResp tpcCount() throws ImageServerException {
      HomeVehicleResp retVal = new HomeVehicleResp();
      HomeVehicleParam param = new HomeVehicleParam();
      param.setType("2");
      Date nowTime = new Date();
      param.setStartTime(DateUtils.truncate(DateUtils.addMonths(nowTime, -1), 5).getTime());
      param.setEndTime(DateUtils.truncate(DateUtils.addDays(nowTime, 1), 5).getTime());
      ResponseData responseData = this.restAssert.getBusinessAssert(BusinessServerId.pvd).postFormData(this.createPvdParam(param), "/pvd/offline/fakeVehicle/detailList");
      if (responseData.code == 200) {
         JSONObject data = responseData.getData();
         JSONArray array = data.getJSONArray("list");
         if (array != null) {
            List list = new ArrayList();
            retVal = new HomeVehicleResp(data.getInteger("count"), list);

            for(int i = 0; i < array.size(); ++i) {
               JSONObject jsonObject = array.getJSONObject(i);
               if (list.size() < param.getPageSize()) {
                  String firstPanoramaImage = jsonObject.getString("firstPanoramaImage");
                  String firstFeatureImage = jsonObject.getString("firstFeatureImage");
                  list.add(new HomeVehicleCount(jsonObject.getString("plateNumber"), jsonObject.getLong("firstPassTime"), firstPanoramaImage, this.getFeatureImg(firstFeatureImage, firstPanoramaImage, jsonObject)));
               }
            }
         }
      }

      return retVal;
   }

   private String cutImg(String panoramaImage, PosVO posVO) throws ImageServerException {
      String fileName;
      if (panoramaImage.contains("filename=")) {
         fileName = panoramaImage.split("filename=")[1];
      } else {
         fileName = panoramaImage;
      }

      return this.imageServerClient.cutImage(fileName, posVO.getLeft(), posVO.getRight(), posVO.getTop(), posVO.getBottom());
   }

   private String getFeatureImg(String featureImage, String panoramaImage, JSONObject jsonObject) throws ImageServerException {
      if (StringUtils.isBlank(featureImage)) {
         PosVO posVO;
         try {
            posVO = (PosVO)jsonObject.getJSONObject("extString1").getJSONObject("Vehicle").getJSONObject("Pos").toJavaObject(PosVO.class);
         } catch (Exception var6) {
            var6.printStackTrace();
            log.error("裁图异常", var6);
            return null;
         }

         featureImage = this.cutImg(panoramaImage, posVO);
      }

      return featureImage;
   }
}
